/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.commandline;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;

import softstarter.gui.master.GUIStateController;
import softstarter.gui.subareas.ButtonFactory;
import softstarter.gui.subareas.FireActionsController;
import softstarter.launching.RunnableItem;

public class CommandLineArea {
    
	private JPanel masterContainer;
	
	private CustomJComboBox commandLine;
	private JButton goButton;

	private CommandLineFireListener commandLineFireListener;
	
	private FireActionsController fireActionsController;	
	
	
	public CommandLineArea(FireActionsController controller) {
		fireActionsController = controller;
	    build();
	}
	
	private void build() {
		masterContainer = new JPanel();
		masterContainer.setOpaque(false);
	    FlowLayout layout = new FlowLayout();
	    layout.setHgap(0);
	    layout.setVgap(0);
	    masterContainer.setLayout(layout);
	    
		commandLine = new CustomJComboBox();
		commandLineFireListener = new CommandLineFireListener(fireActionsController);
		
		commandLine.setEditable(true);
		commandLine.setPreferredSize(new Dimension(350, 25));
		commandLine.getEditor().addActionListener(commandLineFireListener);
		commandLine.getEditor().selectAll();
					    
		JTextComponent editor = (JTextComponent) commandLine.getEditor().getEditorComponent();
	    editor.setDocument(new CommandLineContent(this, fireActionsController));
	    
		masterContainer.setBorder(new EmptyBorder(0, 0,0, 0));
		
		JLabel logo = new ButtonFactory().getImageLabel("/images/Logo.png");
		logo.addMouseListener(new OpenProgramHomePageListener());
		logo.setToolTipText("Open website");
		
		masterContainer.add(logo);
		masterContainer.add(commandLine);
	}
	
	public void populateCommandLineAndShowPopup(List<RunnableItem> topHits) {
		
		commandLine.updateHitList(topHits);
	}
	
	public String getSearchWord() {
	    return commandLine.getEditor().getItem().toString();
	}
	
	public String getItemToBeLaunched() {
				
		if(commandLine.isPopupVisible() && commandLine.getItemCount() > 0) {
			
			RunnableItem firstInTheLine = (RunnableItem)commandLine.getItemAt(0);
			return firstInTheLine.getFullPath();
			
		} else {
			
			return getSearchWord();
		}
		
	}
		
	public JComboBox getCommandLine() {
		return commandLine;
	}
	
	public JButton getGoButton() {
		return goButton;
	}
	
	public JPanel getMasterContainer() {
		return masterContainer;
	}
	
	public void clearTextArea() {
        commandLine.setSelectedItem("");
	}
	
	public void setFocusToCommandLine() {
	    commandLine.getEditor().getEditorComponent().requestFocusInWindow();
	}
}
